//
//  MQEmoticonTools.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/2/3.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class MQEmoticonTools: NSObject {
    
    static let shareTools:MQEmoticonTools = MQEmoticonTools()
    //读取bundle中的info.plist文件
    
    lazy var emoticonBundle:NSBundle = {
        let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        return NSBundle(path: bundlePath)!
    }()
    
    
    //懒加载默认表情
    private lazy var recentEmoticon:[MQEmoticon] = {
        
        //解档
        let result = self.getRecentEmoticon()
        if result != nil {
            return result!
        }else{
            return [MQEmoticon]()
        }
    }()
    //懒加载默认表情数据
    private lazy var defaultEmoticon:[MQEmoticon] = {
        return self.addEmoticonData("default/info.plist")
    }()
    
    //懒加载emoji表情数据
    private lazy var emojiEmoticon:[MQEmoticon] = {
        return self.addEmoticonData("emoji/info.plist")
    }()
    
    //懒加载lxh表情数据
    private lazy var lxhEmoticon:[MQEmoticon] = {
        return self.addEmoticonData("lxh/info.plist")
    }()
    
    lazy var allEmoticons:[[[MQEmoticon]]] = {
       return [
                //最近表情截取的数组
                self.pagesEmoticons(self.recentEmoticon),
                //默认表情截取的数组
                self.pagesEmoticons(self.defaultEmoticon),
                //emoji表情截取的数组
                self.pagesEmoticons(self.emojiEmoticon),
                //lxh表情截取的数组
                self.pagesEmoticons(self.lxhEmoticon)
             ]
    }()
    
    private func pagesEmoticons(emoticons:[MQEmoticon]) -> [[MQEmoticon]]{
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        var result = [[MQEmoticon]]()
        for i in 0..<pageCount{
            let location = i*20
            var length = 20
            if location + length > emoticons.count{
                length = emoticons.count - location
            }
            let range = NSMakeRange(location, length)
            let emoticon = (emoticons as NSArray).subarrayWithRange(range)
            result.append(emoticon as! [MQEmoticon])
        }
        return result
    }
    
    /**
     Description:根据path获取表情数据
     
     :param: path <#path description#>
     
     :returns: <#return value description#>
     */
    private func addEmoticonData(path:String) -> [MQEmoticon]{
        let file = self.emoticonBundle.pathForResource(path, ofType: nil)
//        printfLog(file)
        let emoticonArray = NSArray(contentsOfFile:file!)
        var result = [MQEmoticon]()
        
        
        for dict in emoticonArray!{
            let emoticon = MQEmoticon(dict: dict as! [String : AnyObject])
            emoticon.path = (path as NSString).stringByDeletingLastPathComponent
            result.append(emoticon)
        }
        return result
    }
    
    //保存表情
    func saveRecentEmoticon(emoticon:MQEmoticon){
        //路径
        let path = ((NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last)! as NSString).stringByAppendingPathComponent("emoticon.bundle")
//        recentEmoticon.append(emoticon)
        recentEmoticon.insert(emoticon, atIndex: 0)
        if recentEmoticon.count > 20{
            recentEmoticon.removeLast()
        }
        
        allEmoticons[0][0] = recentEmoticon
        NSKeyedArchiver.archiveRootObject(recentEmoticon, toFile: path)
    }
    
    //解档表情
    func getRecentEmoticon() -> [MQEmoticon]?{
        let path = ((NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!) as NSString).stringByAppendingPathComponent("emoticon.bundle")
        let emoticons = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? [MQEmoticon]
//        allEmoticons[0][0] = emoticons!
        return emoticons
    }
    
    func emoticonWithChs(chs :String?) -> MQEmoticon?{
  
        for emoticon in defaultEmoticon{
            if emoticon.chs == chs{
                return emoticon
            }
        }
        for emoticon in lxhEmoticon{
            if emoticon.chs == chs{
                return emoticon
            }
        }
        return nil
    }
}
